<template>
	<view>
		<u-modal :show="show" :title="title" :content='content' @confirm="confirm"></u-modal>
		 <u-notice-bar :text="text1"></u-notice-bar>
		<vol-alert>
			<view>个人信息</view>
		</vol-alert>
		<view class="form-test">


			<vol-form @input-confirm="inputConfirm" @extraClick="extraClick" @onChange="onChange" :load-key="true"
				ref="form" :form-options.sync="editFormOptions" :formFields.sync="editFormFields">
			</vol-form>



			<view class="btns">
				<!-- <view class="btn">
				<u-button type="primary" @click="reset" shape="circle" text="重置表单"></u-button>
			</view> -->
				<view class="btn">
					<u-button type="primary" @click="vailForm" shape="circle" text="下一步"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				title: '提示',
				content: ' ',
				type: "新增",
				text1: '您可以注册我们的系统，我们帮您找到合适的学生家长，实现共赢！',
				test: {
					a: 1,
					b: 2
				},
				editFormFields: {
					teacherName: "",
					teacherPhone: "",
					teacherResidentId: "",
					teacherGender: "",
					teacherAge: "",
					teacherSchool: "",
					teacherSpeciality: "",
					teacherIdentity: "",
					teachingLength: "",
					teachingMethod: "",
					teachingTime: "",
					teachingPrice: "",
					teachingArea: "",
					teacherAddress: "",
					teachingParentingSubjects: "",
					teacherDescribe: "",
					teacherExperience: "",
					teacherCertificate: "",
					teacherRequirements: "",
					recentLoginTime: "",
					userid: ""

				},
				editFormOptions: [{
						"title": "姓名",
						"required": true,
						"field": "teacherName",
						"maxlength": "11",
					},
					{
						"title": "联系电话",
						"required": true,
						"field": "teacherPhone",
						"maxlength": "11",
						"type": "number"

					}, {
						"title": "身份证号",
						"required": true,
						"field": "teacherResidentId",
						"maxlength": "18",
						"type": "number"

					}, {
						"title": "学校",
						"required": true,
						"field": "teacherSchool",
						"maxlength": "18",

					}, {
						"title": "专业",
						"required": true,
						"field": "teacherSpeciality",
						"maxlength": "18",

					},
					{
						"title": "家教性别",
						"required": true,
						"field": "teacherGender",
						"type": "select",
						"data": [{
							"key": "男",
							"value": "男"
						}, {
							"key": "女",
							"value": "女"
						}],
					},

					{
						"title": "家教科目",
						"required": true,
						"field": "teachingParentingSubjects",
						"type": "selectList",
						"data": [{
							"key": "语文",
							"value": "语文"
						}, {
							"key": "数学",
							"value": "数学"
						}, {
							"key": "英语",
							"value": "英语"
						}],
					},

					{
						"title": "授课方式",
						"field": "teachingMethod",
						"type": "select",
						"required": true,
						"data": [{
							"key": "线上教学",
							"value": "线上教学"
						}, {
							"key": "线下教学",
							"value": "线下教学"
						}],
						// key: "pn"
					},
					{
						"title": "教师身份",
						"field": "teacherIdentity",
						"type": "select",
						"required": true,
						"data": [{
							"key": "研究生",
							"value": "研究生"
						}, {
							"key": "大学生",
							"value": "大学生"
						}],
						// key: "pn"
					},

					{
						"title": "家教费用",
						"field": "teachingPrice",
						type: "select",
						"required": true,
						data: [{
								"key": "80/小时",
								"value": "80/小时"
							}, {
								"key": "90/小时",
								"value": "90/小时"
							},
							{
								"key": "100/小时",
								"value": "100/小时"
							}, {
								"key": "110/小时",
								"value": "110/小时"
							},
							{
								"key": "120/小时",
								"value": "120/小时"
							}, {
								"key": "面议",
								"value": "面议"
							}
						],
						// key: "pn"
					}, {
						"title": "教师年龄",
						"field": "teacherAge",
						"type": "select",
						"required": true,
						data: [{
								"key": "18岁",
								"value": "18岁"
							}, {
								"key": "20岁",
								"value": "20岁"
							},
							{
								"key": "22岁",
								"value": "22岁"
							}, {
								"key": "23岁",
								"value": "23岁"
							},
							{
								"key": "24岁",
								"value": "24岁"
							}, {
								"key": "25岁",
								"value": "25岁"
							}
						],
						// key: "pn"
					},
					{
						"title": "教师教龄",
						"field": "teachingLength",
						"type": "select",
						"required": true,
						"data": [{
								"key": "1年",
								"value": "1年"
							}, {
								"key": "2年",
								"value": "2年"
							},
							{
								"key": "3年",
								"value": "3年"
							}, {
								"key": "4年",
								"value": "4年"
							},
							{
								"key": "5年",
								"value": "5年"
							}, {
								"key": "6年",
								"value": "6年"
							}
						],
						// key: "pn"
					},
					// {
					// 	"title": "家教时间",
					// 	"required": true,
					// 	"field": "teachingTime"
					// },
					{
						"title": "家教区域",
						"required": true,
						"field": "teachingArea",
						type: "selectList",
						data: [{
							"key": "昌平区",
							"value": "昌平区"
						}, {
							"key": "海淀区",
							"value": "海淀区"
						}],
					},
					{
						"title": "居住地址",
						"required": true,
						"field": "teacherAddress"
					}


				],
			}
		},
		methods: {
			confirm() {
				this.show = false;
			},
			extraClick(item) {
				//点击后给表字段设置值
				this.editFormFields.customInput = ~~(Math.random() * 10000000)
				this.$toast('表单按钮点击:' + item.title)
			},
			onChange(field, value, item, data) { //日期与下拉框选择事件
				if (field == "selectClickValue" || field == "dateClickValue") {
					this.$toast(`选择字段${field}值${value}`)
					return;
				}
				if (field == 'switchValue') {
					//获取单选字段配置
					let op = this.editFormOptions.find(c => {
						return c.field == 'radioVal'
					});
					//选择是时，隐藏【单选】字段
					this.$set(op, 'hidden', value + '' === "1")
					return;
				}
			},
			vailForm() {
				// if (!this.changePhone(this.studentPhone)) {
				// 	this.$toast("请输入正确的手机号码")
				// 	return;
				// }
               var  that=this;
				if (this.$refs.form.validate()) {
					
					let UserInfo = this.$store.getters.getUserInfo();
					this.editFormFields.userid = UserInfo.userId;
					if (this.type == "新增") {
						this.editFormFields.auditStatus="未审核"
						this.http.post("wechat/teacher/add", this.editFormFields, "正在递交数据....").then(data => {
							if (data.code == 200) {
								that.http.get("wechat/platformusers/" +this.editFormFields.userid, null,
									false).then(
									result => {
										if (result.code == 200) {
											// that.UserInfo = result.data;
											that.$store.commit("setUserInfo", result.data);
											uni.redirectTo({
												url: '/pages/dz_teacher/dz_teacher_edit_2?id=' + data
													.data // 跳转到新页面
											});
										}

									})



							} else {
								this.content = data.msg;
								this.show = true;
							}
						})
					} else {
						this.editFormFields.auditStatus="未审核"
						this.http.post("wechat/teacher/edit", this.editFormFields, "正在递交数据....").then(data => {
							if (data.code == 200) {


								uni.redirectTo({
									url: '/pages/dz_teacher/dz_teacher_edit_2?id=' + data.data // 跳转到新页面
								});

							} else {
								this.content = data.msg;
								this.show = true;
							}
						})
					}



				}
			},
			reset() {
				this.$refs.form.reset();
				this.$toast("表单已重置")



			},
			changePhone(data) {
				let reg = /^1[3456789]\d{9}$/
				// 座机号校验
				let reg2 = /^0\d{2,3}-?\d{7,8}$/
				if (reg.test(data) || reg2.test(data)) {
					console.log("111")
					return true;
				} else {
					console.log("222")
					return false;
				}
			},

			inputConfirm(field, e) {
				this.$toast(`字段${field}回车事件`)
				console.log(field)
			}
		},
		onShow() {

		},
		onReady() {
			//设置表单默认值请写在onReady中
			// this.editFormFields.inputText = 'vol框架';
			if(this.type=="新增"){
				let position = this.$store.getters.getPosition();
				
				if (position != null) {
					
					 this.editFormFields.teacherAddress=position.address;
					  this.editFormFields.remark3=position.latitude;
					  this.editFormFields.remark4=position.longitude;
				      // console.log("zz",this.editFormFields.studentAddress);
				} 
			}
			
		},
		onLoad() {
			var that = this;
			let UserInfo = this.$store.getters.getUserInfo();
			console.log(UserInfo.teacherId);
			if (UserInfo.teacherId != null && UserInfo.teacherId != "") {
				this.type = "更新"
				var id = UserInfo.teacherId;
				console.log("zz" + id);
				this.http.get("wechat/teacher/" + id, null,
					false).then(
					result => {
						if (result.code == 200) {
							that.editFormFields = result.data;

						}

					})
			} else {
				this.type = "新增"
				

			}
		}
	}
</script>

<style lang="less" scoped>
	.form-test {
		height: auto;
		margin-top: -20rpx;
		background: #f6f6f6;
		padding-top: 20rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
	}

	.btns {
		display: flex;
		padding: 0rpx 20rpx;
		margin-top: 40rpx;

		.btn {
			flex: 1;
			padding: 20rpx;
		}
	}
</style>
